package com.atguigu.search.listener;

import com.atguigu.clients.ProductClient;
import com.atguigu.doc.ProductDoc;
import com.atguigu.pojo.Product;
import lombok.extern.slf4j.Slf4j;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * ClassName:SpringBootListener
 * Package:com.atguigu.search.listener
 * Description:
 *
 * @Author 应永豪
 * @Create 2024/4/22 9:39
 * @Version 1.0
 */
@Component
@Slf4j
public class SpringBootListener implements ApplicationRunner {
//    需要在此方法完成ES的同步
//    1.判断ES中product索引是否存在
//    2.如果不存在就用java代码创建一个
//    3.存在的话就删除原来的树
//    4.查询商品全部数据
//    5.进行ES库的更新工作

    @Autowired
    private RestHighLevelClient restHighLevelClient;
    @Autowired
    private ProductClient productClient;

    private String indexStr = "";


    @Override

    public void run(ApplicationArguments args) throws Exception {
//        判断索引是否存在
        GetIndexRequest product = new GetIndexRequest("product");
        boolean exists = restHighLevelClient.indices().exists(product, RequestOptions.DEFAULT);

        if (exists) {
//            如果存在，则删除原有数据
            DeleteByQueryRequest queryRequest = new DeleteByQueryRequest("product");
            queryRequest.setQuery(QueryBuilders.matchAllQuery());//全部删除
            restHighLevelClient.deleteByQuery(queryRequest, RequestOptions.DEFAULT);
        } else {
//            如果不存在就创建新的索引
            CreateIndexRequest createIndexRequest = new CreateIndexRequest("product");
            createIndexRequest.source(indexStr, XContentType.JSON);
            restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);


        }

//        查询全部商品数据
        List<Product> productList = productClient.allList();
//批量插入数据
        BulkRequest bulkRequest = new BulkRequest();

        ObjectMapper objectMapper = new ObjectMapper();

        for (Product products : productList) {
//            做数据格式转换，转换成ES的数据格式
            ProductDoc productDoc = new ProductDoc(products);
//            用于插入数据
            IndexRequest indexRequest = new IndexRequest("product").id(products.getProductId().toString());
            String json = objectMapper.writeValueAsString(productDoc);
            indexRequest.source(json, XContentType.JSON);
            bulkRequest.add(indexRequest);
        }
        restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
    }
}
